//------------------------------------------------------------------------------
#include <QtDebug>
#include "CProfils.h"
//------------------------------------------------------------------------------
static CProfils * instance=0;
//------------------------------------------------------------------------------
CProfils::~CProfils(void) {}
//------------------------------------------------------------------------------
CProfils * CProfils::getInstance(void) {
	if(instance == 0) {
		instance=new CProfils();
	}
	return instance;
}
//------------------------------------------------------------------------------
void CProfils::write(void) {
	int i, nbProfil;
	
	nbProfil=size();
	
	_settings->setValue("version", _version);
	_settings->setValue("nbProfil", QString().setNum(nbProfil));
						
	for(i=0;i<nbProfil;i++) {
		CProfil *profil=at(i);
		
		if(profil == _currentProfil) {
			_settings->setValue("defaultProfil", QString().setNum(i));
		}
		_settings->setValue("profil_"+QString().setNum(i), profil->getName());
		profil->write();
	}
}
//------------------------------------------------------------------------------
CProfil * CProfils::getProfil(QString name) {
	int i;
	CProfil *profil;
	
	for(i=0;i<size();i++) {
		profil=at(i);
		
		if(profil->getName() == name) {
			return profil;
		}
	}
	
	return 0;
}
//------------------------------------------------------------------------------
CProfils::CProfils(void) {
	_settings=new QSettings("AlpesRessources", "coMexpert");
	_currentProfil=0;
	read();
}
//------------------------------------------------------------------------------
void CProfils::read(void) {
	int i, nbProfil;
	int defaultProfil;
	
	_version=_settings->value("version", "").toString();
	nbProfil=_settings->value("nbProfil", "0").toInt();
	defaultProfil=_settings->value("defaultProfil", "-1").toInt();

	for(i=0;i<nbProfil;i++)	{
		QString name=_settings->value("profil_"+QString().setNum(i), "").toString();
		CProfil *profil=new CProfil(_settings, name);
		
		append(profil);
		
		if(defaultProfil == i) {
			_currentProfil=profil;
		}
	}
}
//------------------------------------------------------------------------------